package pt.ua.helpdesk.client;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * This class extends a composite and will allow the
 * user to add a ticket.
 *  
 * @author Amigos do Noddy
 */
public class TicketAdd extends Composite {

	/**
	 * Class constructor
	 *
	 */
	public TicketAdd() {

		final HorizontalPanel horizontalPanel = new HorizontalPanel();
		initWidget(horizontalPanel);
		horizontalPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		horizontalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

		final VerticalPanel verticalPanel = new VerticalPanel();
		horizontalPanel.add(verticalPanel);
		verticalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);

		final HorizontalPanel horizontalPanel_1 = new HorizontalPanel();
		verticalPanel.add(horizontalPanel_1);
		horizontalPanel_1.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);

		final Label lblTitle = new Label("Título: ");
		horizontalPanel_1.add(lblTitle);
		lblTitle.setStyleName("label");

		final TextBox txtTitle = new TextBox();
		horizontalPanel_1.add(txtTitle);
		txtTitle.setWidth("300");
		txtTitle.setStyleName("textbox");

		final HorizontalPanel horizontalPanel_2 = new HorizontalPanel();
		verticalPanel.add(horizontalPanel_2);

		final Label lblQuestion = new Label("Pergunta: ");
		horizontalPanel_2.add(lblQuestion);
		lblQuestion.setStyleName("label");

		final TextArea txtQuestion = new TextArea();
		horizontalPanel_2.add(txtQuestion);
		txtQuestion.setSize("300", "200");
		txtQuestion.setStyleName("textarea");

		final HorizontalPanel horizontalPanel_3 = new HorizontalPanel();
		verticalPanel.add(horizontalPanel_3);
		
		final Label lblAlerta = new Label("");
		horizontalPanel_3.add(lblAlerta);
		lblAlerta.setStyleName("erro");
		
		
		
		final Button btnSubmit = new Button();
		horizontalPanel_3.add(btnSubmit);
		btnSubmit.setStyleName("button");
		
		btnSubmit.addClickListener(new ClickListener() {
			public void onClick(final Widget sender) {
				if((txtQuestion.getText().equals(""))||(txtTitle.getText().equals("")))
				{
					lblAlerta.setText("Tem de preencher todos os campos.");					
				}
				else
				{
					
					ServiceTickets.Util.getInstance().addTicket(txtTitle.getText(),txtQuestion.getText(),new Integer(1)/*Pending*/,new Integer(1)/*Active*/, new Integer((String)Default.getSession("IDPerson")), new TicketAddAsyncCallback(lblAlerta));
					txtQuestion.setText("");
					txtTitle.setText("");
				}
			}
		});
		btnSubmit.setText("Submeter");

		
	}
	
	/**
	 * AsyncCallback responsible for displaying feedback on whether the comment was added successfully or not 
	 * 
	 * @author Amigos do Noddy
	 *
	 */
	static class TicketAddAsyncCallback implements AsyncCallback {
		  Label label;
		  
		  /**
		   * Constructor
		   * 
		   * @param label Label where feedback is displayed
		   */
		  
		  public TicketAddAsyncCallback(Label label) {
			  this.label= label;
		  }
		  
		  /**
		   * Success handler
		   */
		  public void onSuccess(Object object) {
			  Boolean OK = (Boolean)object;
			  String message;
	    	  if(OK.booleanValue())
	    	  {
	    		  message = "Dados submetidos com sucesso";
	    		  DOM.setInnerHTML(label.getElement(), message);
	    	  }
	    	  else
	    	  {
	    		  message = "Ocorreu um erro inesperado";
	    		  DOM.setInnerHTML(label.getElement(), message);
	    	  }
		  }
		  
		  /**
		   * Failure handler
		   */
		  public void onFailure(Throwable throwable) {
			  System.out.println("FALHOU!");
		  }
		}

}
